﻿using System;
using System.Linq;
using System.Runtime.Remoting.Messaging;

namespace OperationPlugins.Applications.Models
{
    /// <summary>
    /// The user account.
    /// </summary>
    [Serializable]
    public sealed class Account : MarshalByRefObject, ILogicalThreadAffinative
    {
        /// <summary>
        /// Is anonymous authentication enabled?
        /// </summary>
        public bool IsAnonymous { get; set; }

        /// <summary>
        /// The username.
        /// </summary>
        public string Username { get; set; }

        /// <summary>
        /// The password.
        /// </summary>
        public string Password { get; set; }

        public static bool IsAuthorized(string username, string password, AppInstance app)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }

            return app.Accounts.Any(x => x.IsAnonymous || (x.Username == username && x.Password == password));
        }

    }
}
